/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.dao.impl;

import com.tesam.gedeon.dao.UnitDao;
import com.tesam.gedeon.entity.Unit;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Sistema
 */
@Repository
public class UnitDaoImpl extends SimpleJdbcDaoSupport implements UnitDao {

    @Autowired
    public UnitDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    }
    
    @Override
    public int getCountAll() {
       return getJdbcTemplate().queryForInt("select count(*) from unit where deleted = 0");
    }

    @Override
    public int getCountBySearch(Unit unit) {
        return getJdbcTemplate().queryForInt("select count(*) from unit where deleted = 0 and upper(name) like '%"+unit.getName().toUpperCase()+"%'" );
    }

    @Override
    public List<Unit> getListByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        List<Unit> result = new ArrayList();
            try{
                result = getJdbcTemplate().query("select id , createdby , code , name , datecreated "
                        + "from unit where deleted = 0 "+
                        "order by  "+dataToSort+" "+directionOfOrder+" limit "+endLimit+" offset "+startLimit, new BeanPropertyRowMapper<Unit>(Unit.class));
                 
            }catch(Exception e){
                System.out.println("getListByLimit : "+e.getMessage());
            }
        return result ;
    }

    @Override
    public List<Unit> getListBySearch(int startLimit, int endLimit, String dataToSort, String directionOfOrder, Unit unit) {
       List<Unit> result = new ArrayList();
            try{
                result = getJdbcTemplate().query("select id , createdby , code , name , datecreated "
                        + "from unit where deleted = 0 and upper(name) like '%"+unit.getName().toUpperCase()+"%' "+
                        "order by  "+dataToSort+" "+directionOfOrder+" limit "+endLimit+" offset "+startLimit, 
                        new BeanPropertyRowMapper<Unit>(Unit.class));
            
                
            }catch(Exception e){
                System.out.println("getListByLimit : "+e.getMessage());
            }
        return result ;
    }

    @Override
    public void delete(Unit unit) {
     getJdbcTemplate().update("update unit set deleted = 1 where id = ?", unit.getId());
    }

    @Override
    public int saveUnit(Unit unit) {
        int id = 0 ;
        try{
            getJdbcTemplate().update("insert into unit "
                    + "(createdby ,  datecreated , status , deleted , "
                    + "presence , ownerid , code , name) values ("
                    + "? , NOW() , 1 , 0 , "
                    + "0 , ? , ? , ?)" , 
                    unit.getCreatedBy() , unit.getOwnerId() , unit.getCode() , unit.getName());
            id = getSimpleJdbcTemplate().queryForInt("SELECT last_value FROM entity_id_seq");
            
        }catch(Exception e){
        System.out.println("saveUnit : "+e.getMessage());
        }
        return id ;
    }

    @Override
    public int getExistCode(Unit unit){
        int id = 0 ;
            try{
                id = getJdbcTemplate().queryForInt("select count(*) as all from unit where deleted = 0 and upper(code) = ?",unit.getCode().toUpperCase());
            }catch(Exception e){
            System.out.println("getExistCode : "+e.getMessage());
            }
        return id ;
    }
    
    @Override
    public void update(Unit unit) {
        try{
 
        getJdbcTemplate().update("update unit set datemodified = NOW() , modifiedby = ? , code = ? , "
                + "name = ?  where id = ? " , unit.getModifiedBy() , unit.getCode() , unit.getName() , unit.getId());
        }catch(Exception e){
        System.out.println("update : "+e.getMessage());
        }
    }
    
    @Override
     public List<Unit> getUnitById(Unit unit){
        List<Unit> result  = new ArrayList();
        
        try{
            getJdbcTemplate().update("update unit set viewedtime = NOW() where id = ?" , unit.getId());
            
            result = getJdbcTemplate().query("select id , createdby , code , name , datecreated "
                        + "from unit where deleted = 0 and id = ?", 
                    new BeanPropertyRowMapper<Unit>(Unit.class) ,
                    unit.getId());
        }catch(Exception e){
        System.out.println("getUnitById : "+e.getMessage());
        }
        
        return result ;
    }
    
}
